package cn.cocho.custom.yunCashierDf.merchant.controller;

import cn.cocho.common.web.app.JsonResultContants;
import cn.cocho.common.web.character.StringUtils;
import cn.cocho.common.web.jqgrid.JQGridContants;
import cn.cocho.common.web.jqgrid.JQGridPage;
import cn.cocho.common.web.jqgrid.JQGridUtils;
import cn.cocho.custom.yunCashierDf.common.controller.QRCodeUtil;
import cn.cocho.custom.yunCashierDf.merchant.domain.MerchantInfo;
import cn.cocho.custom.yunCashierDf.merchant.service.MerchantInfoService;
import cn.cocho.custom.yunCashierDf.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by WQJ on 2017-05-23.
 */
@Controller
public class MerchantInfoController {

    @Autowired
    private MerchantInfoService merchantInfoService;

    @ResponseBody
    @RequestMapping(value = "/admin/merchant/getMerchantInfoList")
    public JQGridPage getMerchantInfoList(JQGridPage<MerchantInfo> pageJQGrid) {
        return merchantInfoService.getListByJQgrid(pageJQGrid);
    }

    @ResponseBody
    @RequestMapping(value = "/admin/merchant/editMerchantInfo")
    public void editMerchantInfo(MerchantInfo merchantInfo, HttpServletResponse response) {
        String warnMessage = null;
        if (merchantInfo.getOper().equals(JQGridContants.EDIT_OPER_ADD)) {
            merchantInfoService.add(merchantInfo);
        } else if (merchantInfo.getOper().equals(JQGridContants.EDIT_OPER_UPDATE)) {
            merchantInfo.setMerchantId(merchantInfo.getId());
            merchantInfoService.update(merchantInfo);
        } else if (merchantInfo.getOper().equals(JQGridContants.EDIT_OPER_DEL)) {
            JQGridUtils.delete("delete from merchant_info where merchant_id ", merchantInfo.getId());
        }
        JQGridUtils.warnUserMessage(warnMessage, response);
    }

    @ResponseBody
    @RequestMapping(value = "/admin/merchant/getMerchantInfoById", method = RequestMethod.POST)
    public JsonResult<MerchantInfo> getMerchantInfoById(String merchantId) {
        JsonResult<MerchantInfo> result;
        MerchantInfo merchantInfo = merchantInfoService.getMerchantInfoById(merchantId);
        if (merchantInfo != null) {
            result = new JsonResult<MerchantInfo>(JsonResultContants.SUCCESS, "获取商户信息成功！", merchantInfo);
        } else {
            result = new JsonResult<MerchantInfo>(JsonResultContants.FAIL, "无法获取商户信息！");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/admin/merchant/saveMerchantInfo", method = RequestMethod.POST)
    public JsonResult saveMerchantInfo(MerchantInfo merchantInfo) {
        JsonResult result;
        if (StringUtils.isEmpty(merchantInfo.getMerchantId())) { //新增
            boolean insertResult = merchantInfoService.insertMerchantInfo(merchantInfo);
            if (insertResult == true) {
                result = new JsonResult(JsonResultContants.SUCCESS, "新增商户信息成功！", merchantInfo.getMerchantId());
            } else {
                result = new JsonResult(JsonResultContants.FAIL, "新增商户信息失败！");
            }
        } else {  //修改
            boolean replaceResult = merchantInfoService.replaceMerchantInfo(merchantInfo);
            if (replaceResult) {
                result = new JsonResult(JsonResultContants.SUCCESS, "修改商户信息成功！", merchantInfo.getMerchantId());
            } else {
                result = new JsonResult(JsonResultContants.FAIL, "修改商户信息失败！");
            }
        }
        return result;
    }
}
